/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.daos;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.models.Promotion;
import com.insurance.cis.scos.PromotionSearchCondition;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TruongLQ
 */
public class PromotionDao {
    
    public List<Promotion> getPromotions(PromotionSearchCondition sco) {
        List<Promotion> promotions = new ArrayList<>();

        final StringBuilder sql = new StringBuilder("select * from Promotion");
        boolean first = false;
        Map<Integer, Object> parameters = new HashMap<>();
        int count = 0;
        if (sco == null || !sco.isEmptyCondition()) {
            sql.append(" where ");
            if (sco.getPromotionID() != null) {
                sql.append("PromotionID = ? ");
                first = true;
                parameters.put(++count, sco.getPromotionID());
            }
            if (sco.getDiscount() != null) {
                if (first) {
                    sql.append(" and ");
                }
                first = true;
                sql.append("Discount <= ?");
                parameters.put(++count, sco.getDiscount());
            }
            if (sco.getGift()!=null &&!sco.getGift().isEmpty()) {
                if (first) {
                    sql.append(" and ");
                }
                sql.append("Gift like ?");
                parameters.put(++count, "%"+sco.getGift()+"%");
            }
        }

        try {
            PreparedStatement pst = CisConnection.getInstance()
                    .getSqlConnection().prepareStatement(sql.toString());
            for (Integer pos : parameters.keySet()) {
                pst.setObject(pos, parameters.get(pos));
            }
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                promotions.add(new Promotion(rs.getInt(1), 
                        rs.getInt(2), rs.getString(3)));
            }
            return promotions;
        } catch (SQLException ex) {
            Logger.getLogger(PromotionDao.class.getName()).log(Level.SEVERE, null, ex);
            return promotions;
        }

    }
    
    public boolean checkIDExist(String promotionID) {
        final String SQL = "select * from Promotion where PromotionID=?";
        try {
            PreparedStatement pst = CisConnection.getInstance().getSqlConnection()
                    .prepareStatement(SQL);
            pst.setString(1, promotionID);
            ResultSet rs = pst.executeQuery();
            return rs.next();
        } catch (SQLException ex) {
            Logger.getLogger(PromotionDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
